#pragma once
#include <iostream>
#include <pthread.h>
#include <string>
#include <functional>
#include "../common/logmessage.hpp"
#include "../common/nocopy.hpp"

typedef void *(*thread_func)(void *);

class MyThread: public NoCopy
{
    pthread_t _tid;
    thread_func _callback = nullptr;
    void *_args = nullptr;

public:
    MyThread() {}

    MyThread(thread_func callback, void *args)
        : _callback(callback),
          _args(args)
    {
        pthread_create(&_tid, nullptr, _callback, _args);
    }

    void run(thread_func callback, void *args)
    {
        _callback = callback;
        _args = args;
        pthread_create(&_tid, nullptr, _callback, _args);
        // printf("[%d] run\n", _tid%10000);
        LogMessage(DEBUG, "[%d] run", _tid % 10000);
    }

    void join()
    {
        // printf("[%d] join\n", _tid%10000);
        LogMessage(DEBUG, "[%d] join", _tid % 10000);
        pthread_join(_tid, nullptr);
    }

    pthread_t get_id()
    {
        return _tid;
    }
};
